package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.TransactionRiskMonitoring;
import com.example.mall.dto.TransactionRiskMonitoringDto;
import com.example.mall.mapper.TransactionRiskMonitoringMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.TransactionRiskMonitoringQuery;
import com.example.mall.service.ITransactionRiskMonitoringService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 交易风险监控表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class TransactionRiskMonitoringServiceImpl extends ServiceImpl<TransactionRiskMonitoringMapper, TransactionRiskMonitoring> implements ITransactionRiskMonitoringService {

    @Autowired
    private TransactionRiskMonitoringMapper transactionRiskMonitoringMapper;

    @Override
    public PageDTO<TransactionRiskMonitoringDto> queryPage(TransactionRiskMonitoringQuery transactionRiskMonitoringQuery) {
        if (transactionRiskMonitoringQuery == null) {
            transactionRiskMonitoringQuery = new TransactionRiskMonitoringQuery();
        }
        Page<TransactionRiskMonitoring> page = transactionRiskMonitoringQuery.toMpPageByDefaultCreateTimeAsc();
        Page<TransactionRiskMonitoring> cartPage = lambdaQuery()
                .like(transactionRiskMonitoringQuery.getMonitorId()!= null, TransactionRiskMonitoring::getMonitorId, transactionRiskMonitoringQuery.getMonitorId())
                .like(transactionRiskMonitoringQuery.getRiskLevel()!= null, TransactionRiskMonitoring::getRiskLevel, transactionRiskMonitoringQuery.getRiskLevel())
                .like(transactionRiskMonitoringQuery.getRiskReason()!= null, TransactionRiskMonitoring::getRiskReason, transactionRiskMonitoringQuery.getRiskReason())
                .like(transactionRiskMonitoringQuery.getOrderId()!= null, TransactionRiskMonitoring::getOrderId, transactionRiskMonitoringQuery.getOrderId())
                .like(transactionRiskMonitoringQuery.getUserId()!= null, TransactionRiskMonitoring::getUserId, transactionRiskMonitoringQuery.getUserId())
                .orderByDesc(TransactionRiskMonitoring::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, TransactionRiskMonitoringDto.class);
    }

    @Override
    public TransactionRiskMonitoring queryTransactionRiskMonitoringById(Long id) {
        AssertUtil.isNotNull(id, "id不能为空");
        return transactionRiskMonitoringMapper.selectById(id);
    }

    @Override
    public List<TransactionRiskMonitoring> queryTransactionRiskMonitoringList() {
        return this.list();
    }

    @Override
    public void insert(TransactionRiskMonitoringDto transactionRiskMonitoringDto) {
        TransactionRiskMonitoring transactionRiskMonitoring = new TransactionRiskMonitoring();
        CopyUtils.copyProperties(transactionRiskMonitoringDto, transactionRiskMonitoring);
        transactionRiskMonitoring.setMonitorId(CodeGenerateUtils.generateUniqueId());
        transactionRiskMonitoring.setCreateDate(new Date());
        transactionRiskMonitoring.setUpdateDate(new Date());
        transactionRiskMonitoringMapper.insert(transactionRiskMonitoring);
    }

    @Override
    public void update(TransactionRiskMonitoringDto transactionRiskMonitoringDto) {
        AssertUtil.isNotNull(transactionRiskMonitoringDto.getMonitorId(), "id不能为空");
        TransactionRiskMonitoring transactionRiskMonitoring = transactionRiskMonitoringMapper.selectById(transactionRiskMonitoringDto.getMonitorId());
        AssertUtil.isNotNull(transactionRiskMonitoring, "交易风险监控表不存在");
        CopyUtils.copyProperties(transactionRiskMonitoringDto, transactionRiskMonitoring);
        transactionRiskMonitoring.setUpdateDate(new Date());
        transactionRiskMonitoringMapper.updateById(transactionRiskMonitoring);
    }

    @Override
    public void delete(Long id) {
        AssertUtil.isNotNull(id, "id不能为空");
        TransactionRiskMonitoring transactionRiskMonitoring = transactionRiskMonitoringMapper.selectById(id);
        AssertUtil.isNotNull(transactionRiskMonitoring, "交易风险监控表不存在");
        transactionRiskMonitoringMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        AssertUtil.isNotNull(ids, "id不能为空");
        List<TransactionRiskMonitoring> transactionRiskMonitorings = transactionRiskMonitoringMapper.selectBatchIds(ids);
        AssertUtil.isNotNull(transactionRiskMonitorings, "交易风险监控表不存在");
        transactionRiskMonitoringMapper.deleteBatchIds(ids);
    }
}
